<?php
/**
 * Created by PhpStorm.
 * User: xiangfeng
 * Date: 2022/4/11
 * Time: 7:18 AM
 */

namespace app\api\service;


use think\Service;
use OSS\Core\OssException;
use OSS\Core\OssUtil;
use OSS\OssClient;
use think\Image;
use WeChat\Contracts\Tools;

class FileService extends Service
{

    /**
     * @param $getFile
     * @param $path
     * @param int $type
     * @param $nickname
     * @param int $width
     * @param int $height
     * @return mixed
     */
    public static function uploadImageFile($getFile, $path, $type = 0, $nickname, $width = 150, $height = 150)
    {
        #配置OSS基本配置
        $config = array(
            'KeyId' => ALI_ACCESS_KEY_ID,
            'KeySecret' => ALI_ACCESS_KEY_SECRET,
            'Endpoint' => ALI_ENDPOINT,
            'Bucket' => BUCKET,
        );
        $paths = DEFAULT_IMAGE_PATH . $path;
        foreach ($getFile as $name) {
            $image = Image::open($name);
            $pic_name = Tools::createNoncestr(16) . '.' . $image->type();
            $root = $_SERVER['DOCUMENT_ROOT'] . "/static/";
            if (!file_exists($root)) {
                mkdir($root);
            }
            //默认等比例图片压缩
            if ($image->width() > 1000 || $image->height() > 1200) {
                $image->thumb($width, $height, Image::THUMB_SCALING)->save('./static/' . $pic_name);
                $height = $image->height();
                $width = $image->width();
            } else {
                $height = $image->height();
                $width = $image->width();
            }
            //除头像以外都打水印
            $font_file = $_SERVER['DOCUMENT_ROOT'] . '/msyh.ttf';
            if ($type != 1) {
                if ($width != 640 && $width > 100) {
                    $per = round($width / 640, 2);
                    $font = round($per * 14);
                    $image->text('' . $nickname, $font_file, $font, '#ffffff', Image::WATER_SOUTHEAST, [-40, -20])->save('./static/' . $pic_name);
                } else if ($width == 640) {
                    $font = 14;
                    $image->text('' . $nickname, $font_file, $font, '#ffffff', Image::WATER_SOUTHEAST, [-40, -20])->save('./static/' . $pic_name);
                }
            } else {
                $image->save('./static/' . $pic_name);
            }
            try {
                $ossClient = new OssClient($config['KeyId'], $config['KeySecret'], $config['Endpoint']);
                $ossClient->uploadFile($config['Bucket'], $paths . $pic_name, $root . $pic_name);
                unlink('./static/' . $pic_name);
                $ret['file_url'] = DEFAULT_IMAGE_URL . '/' . $paths . $pic_name;
                $ret['file_path'] = $paths . $pic_name;
                $ret['width'] = $width;
                $ret['height'] = $height;
                $ret['file_type'] = $image->type();
                $result['msg'][] = $ret;
                $result['code'] = 200;

            } catch (OssException $e) {
                $result['msg'] = $e->getMessage();
                $result['code'] = 400;
            }
        }
        return $result;
    }

}